package com.h117121.fashionshop.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.dom.Account;
import com.h117121.fashionshop.core.dom.Account.Sex;
import com.h117121.fashionshop.core.exception.EntityNotFoundException;
import com.h117121.fashionshop.validator.UpdateAccountValidator;

@SessionAttributes({ "account" })
@Controller
@RequestMapping("/account")
public class AccountController extends GenericController {
	@Autowired
	private UpdateAccountValidator validator;

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, ModelMap model) {
		if (request.getSession().getAttribute("account") == null) {
			return "redirect:/login";
		} else {
			Account account = (Account) request.getSession().getAttribute(
					"account");
			if (account.getUserName().startsWith("fb")
					|| account.getUserName().startsWith("openid")) {
				model.addAttribute("externalAccount", true);
			}
			model.addAttribute("accountInfo", account);
			return "AccountPage";
		}
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processFormSubmit(HttpServletRequest request,
			@ModelAttribute("accountInfo") Account account, BindingResult errors) {
		if (account.getUserName().startsWith("fb")
				|| account.getUserName().startsWith("openid")) {
			request.setAttribute("externalAccount", true);
		}
		validator.validate(account, errors);
		if (!errors.hasErrors()) {
			Account updatedAccount = null;
			try {
				updatedAccount = businessService.updateAccount(account);
			} catch (EntityNotFoundException e) {
				errors.reject("notfound.updateaccount");
			}

			request.getSession().setAttribute("account", updatedAccount);
			request.setAttribute("successfulMessage", true);
		}

		return "AccountPage";
	}

	@ModelAttribute("sexs")
	public Map<Sex, String> populateSex() {
		Map<Sex, String> sexs = new LinkedHashMap<Sex, String>();
		for (Sex item : Sex.values()) {
			sexs.put(item, item.name());
		}
		return sexs;
	}
}
